package com.whfc.ms.api.sys.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.RandomUtil;
import com.whfc.fuum.dto.SysAlgorithmTypeDTO;
import com.whfc.fuum.param.SysAlgorithmTypeAddParam;
import com.whfc.fuum.param.SysAlgorithmTypeEditParam;
import com.whfc.fuum.service.SysAlgorithmService;
import com.whfc.ms.api.sys.service.MsAlgorithmService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2020-12-10 19:06
 */
@Service
public class MsAlgorithmServiceImpl implements MsAlgorithmService {

    /**
     * 系统算法图片路径
     */
    private String sysAlgTypeImgPath = "sys/alg-type-img";

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;

    @DubboReference(interfaceClass = SysAlgorithmService.class, version = "1.0.0")
    private SysAlgorithmService sysAlgorithmService;

    @Override
    public void addAlgorithmType(SysAlgorithmTypeAddParam param) {
        sysAlgorithmService.addAlgorithmType(param);
    }

    @Override
    public void editAlgorithmType(SysAlgorithmTypeEditParam param) {
        sysAlgorithmService.editAlgorithmType(param);
    }

    @Override
    public void modifyAlgorithmState(Integer id, Integer state) {
        sysAlgorithmService.modifyAlgorithmState(id, state);
    }

    @Override
    public void deleteAlgorithmType(Integer id) {
        sysAlgorithmService.deleteAlgorithmType(id);
    }

    @Override
    public PageData<SysAlgorithmTypeDTO> getAlgorithmTypeList(String keyword, Integer state, Integer pageNum, Integer pageSize) {
        return sysAlgorithmService.getAlgorithmTypeList(keyword, state, pageNum, pageSize);
    }

    @Override
    public List<SysAlgorithmTypeDTO> getAlgorithmTypeList(String keyword, Integer state) {
        return sysAlgorithmService.getAlgorithmTypeList(keyword, state);
    }

    @Override
    public String uploadImg(MultipartFile file) {
        String upload = null;
        //文件上传
        if (file != null) {
            try {
                String fileName = RandomUtil.getRandomFileName() + ".png";
                String ossKey = filePathConfig.getFilePath(sysAlgTypeImgPath, fileName);
                upload = fileHandler.upload(ossKey, file.getInputStream());
            } catch (Exception e) {
                throw new BizException(ResultEnum.FAILURE.getCode(), "发生错误，上传失败");
            }
        }
        if (upload != null) {
            return upload;
        } else {
            throw new BizException(ResultEnum.FAILURE.getCode(), "发生错误，上传失败");
        }
    }
}
